<template>
  <div class="login-container">

    <!-- 登录 -->
    <el-form v-if="isOK" key="login" ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <!-- logo -->
      <div class="title-container">
        <h3 class="title">居家房产后台登录系统</h3>
      </div>

      <!-- 登录表单 -->
      <el-row>
        <el-form-item prop="identifier">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="identifier"
            v-model="loginForm.identifier"
            placeholder="请输入账号"
            name="identifier"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入 6-12 位密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>

        <!-- 登录按钮 -->
        <el-row class="login-btn-wrap">
          <el-button class="login-btn" :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>
          <el-button class="login-btn" :loading="loading" style="width:100%;margin-bottom:30px;" @click.native.prevent="registerTab">注册</el-button>
        </el-row>
      </el-row>

      <!-- 账号密码提示 -->
      <div class="tips">
        <span style="margin-right:20px;">账号：admin</span>
        <span>密码：adminadmin</span>
      </div>
    </el-form>

    <!-- 注册 -->
    <el-form v-else key="register" ref="registerForm" :model="registerForm" :rules="registerRules" class="login-form" auto-complete="on" label-position="left">
      <!-- logo -->
      <div class="title-container">
        <h3 class="title">居家房产后台登录系统</h3>
      </div>

      <!-- 注册表单 -->
      <el-row>
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="registerForm.username"
            placeholder="请输入用户名"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>
        <el-form-item prop="email">
          <span class="svg-container">
            <svg-icon icon-class="example" />
          </span>
          <el-input
            ref="email"
            v-model="registerForm.email"
            placeholder="请输入邮箱"
            name="email"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="registerForm.password"
            :type="passwordType"
            placeholder="6-12 位密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleRegister"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>

        <!-- 注册按钮 -->
        <el-row class="login-btn-wrap">
          <el-button class="login-btn" :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleRegister">注册</el-button>
        </el-row>
      </el-row>
    </el-form>

  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      isOK: true,
      // 登录数据
      loginForm: {
        identifier: '123456',
        password: '123456'
      },
      // 注册数据
      registerForm: {
        username: '',
        email: '',
        password: ''
      },

      // 登录校验
      loginRules: {
        identifier: [
          { required: true, message: '账号必填', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码必填', trigger: 'blur' },
          { min: 6, max: 12, message: '密码必须在 6-12 位之间', trigger: 'blur' }
        ]
      },
      // 注册校验
      registerRules: {
        username: [
          { required: true, message: '用户名必填', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱必填', trigger: 'blur' },
          { pattern: '^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$', message: '请输入正确的邮箱', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码必填', trigger: 'blur' },
          { min: 6, max: 12, message: '密码必须在 6-12 位之间', trigger: 'blur' }
        ]
      },

      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },

    // 点击登录跳转首页
    async handleLogin() {
      try {
        // 校验表单
        await this.$refs.loginForm.validate()
        console.log('校验成功')
        // 发起请求 获取token
        // const res = await login(this.loginForm)
        // console.log(res.data)
        // console.log('登录请求成功')

        // 已经将登录请求逻辑封装在 vuex 所以直接调用 action 即可
        await this.$store.dispatch('user/login', this.loginForm)
        // 提示用户
        this.$message.success('登录成功')
        // 跳转首页
        this.$router.push('/')
      } catch (error) {
        // 失败
        console.log('校验失败')
        // 提示用户
        this.$message.error('账号或密码不正确')
      }
    },

    // 点击切换注册框
    registerTab() {
      this.isOK = false
    },
    // 注册
    async handleRegister() {
      try {
        // 校验表单
        await this.$refs.registerForm.validate()
        // 发起请求
        await this.$store.dispatch('user/register', this.registerForm)
        this.isOK = true
        console.log('注册成功')
        this.$message.success('注册成功')
      } catch (error) {
        // 失败
        console.log('注册失败')
        // 提示用户
        this.$message.error('用户名邮箱不正确')
      }
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #000;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  background-image: url('~@/assets/images/login-bg.jpg');
  background-position: center;
  .el-form-item {
    background: transparent !important;
    border: 1px solid #ccc !important;
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $bg;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        transition-delay: 99999s;
        transition: color 99999s ease-out, background-color 99999s ease-out;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }

  .login-btn-wrap {
    display: flex;
    .login-btn {
      height: 52px;
      font-size: 20px;
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  height: 100vh;
  width: 100%;
  background-image: url("~@/assets/images/login-bg.jpg");
  background-size: 100% 100%;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    background-color: rgba(255, 255, 255, 0.97);
    max-width: 100%;
    margin-top: 160px !important;
    padding: 30px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #ccc;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $bg;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
